package com.shopping.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.shopping.dao.OrderDao;
import com.shopping.pojo.Order;
import com.shopping.pojo.User;
import com.shopping.utils.C3p0Utils;

public class OrderDaoImpl implements OrderDao {

	@Override
	public ArrayList<Order> findAll() {
		ArrayList<Order> list = new ArrayList<Order>();
		QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "select * from `order`";
		try {
			list = (ArrayList<Order>) qr.query(sql, new BeanListHandler<>(Order.class));
			return list;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void main(String[] args) {
		OrderDaoImpl od = new OrderDaoImpl();
//		ArrayList<Order> list = od.findAll();
//		for(Object obj : list) {
//			System.out.println(obj);
//		}
//		BigDecimal bd = new BigDecimal("19999");
//		Order order = new Order("xiangwang","149","shouji",bd,1,new Date(),"ha");
//		System.out.println(order);
//		boolean b = od.insertOrder(order);
		
//		Order order = new Order("xiangwang","149","shouji",200.0,1,new Date(),"ha");
//		order.setO_id(1000);
//		boolean b = od.update(order);
		
		boolean b = od.delete(1029);
		System.out.println(b);
		
	}


	@Override
	public boolean insertOrder(Order order) {
		// 创建QueryRunner对象
		QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource());
		// 写SQL语句
		String sql = "insert into `order`(o_name,o_phone,o_content,o_money,o_status,o_pay_method,o_time,o_description) value(?,?,?,?,?,?,?,?)";
		// 调用方法
		int num;
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sd.format(order.getO_time());
		try {
			num = runner.update(sql, new Object[] {order.getO_name(),order.getO_phone(),order.getO_content()
					,order.getO_money(),order.getO_status(),order.getO_pay_method(),date,order.getO_description()});
			if(num > 0)
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}


	@Override
	public ArrayList<Order> findPage(int page, int limit) {
		ArrayList<Order> list = new ArrayList<Order>();
		QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "select * from `order` limit " + (page-1)*limit + "," + limit;
		System.out.println(sql);
		try {
			list = (ArrayList<Order>) qr.query(sql, new BeanListHandler<>(Order.class));
			return list;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	@Override
	public long TotalData() {
		QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "select count(*) from `order`";
		long sum = 0;
		try {
			sum = (long) qr.query(sql, new ScalarHandler());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return sum;
	}


	@Override
	public boolean update(Order order) {
		QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "update `order` set o_name=?,o_phone=?,o_content=?,o_money=?,o_status=?,o_pay_method=?,o_time=?,o_description=? where o_id =?";
		int i=0;
		try {
			i = qr.update(sql, order.getO_name(),order.getO_phone(),order.getO_content()
					,order.getO_money(),order.getO_status(),order.getO_pay_method()
					,order.getO_time(),order.getO_description(),order.getO_id());
			if(i>0)
				return true;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	@Override
	public boolean delete(long id) {
		QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "delete from `order` where o_id=?";
		int i;
		try {
			i = qr.update(sql, id);
			if(i>0)
				return true;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
}
